package net.xuele.learn.flux;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Future的问题
 *  1、阻塞性，如代码所示，如果多个任务提交后，返回的Future逐一调用get方法时，将会依次阻塞
 *  2、不能链式处理，cf解决了这个问题
 */
public class FutureBlockingDataLoader  extends DataLoader{

    @Override
    protected void doLoad() {

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        runCompletely(executorService.submit(super::loadConfigurations));
        runCompletely(executorService.submit(super::loadUsers));
        runCompletely(executorService.submit(super::loadOrders));
        executorService.shutdown();
    }

    private void runCompletely(Future<?> f) {
        try {
            f.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new FutureBlockingDataLoader().load();
    }
}
